package easypsync.app;

import java.text.DateFormat;
import java.util.Date;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class PIMSync {
	private static final String TAG = PIMSync.class.getName();

	private Context context;

	private static final int SYNC_CONTACTS = 1;
	private static final int SYNC_CALENDAR = 2;
	private static final int SYNC_FOLDER   = 3;
	private static final int SYNC_DONE   = 4;

	public PIMSync(Context context) {
		this.context = context;
	}

	private void showNotification(int id, String tickerText, String contentTitle, String contentText) {
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
		
		int icon = android.R.drawable.arrow_up_float;
		long when = System.currentTimeMillis();

		Notification notification = new Notification(icon, tickerText, when);
		
		Intent notificationIntent = new Intent();
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

		mNotificationManager.notify(id, notification);
	}
	
	public void doSync() {

        Toast.makeText(context, "Synchronization started", Toast.LENGTH_LONG).show();
		// Apply Settings
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		SMBTransfer.setTargetHost(prefs.getString("target_host", null));
		SMBTransfer.setTargetUser(prefs.getString("target_user", null));
		SMBTransfer.setTargetPass(prefs.getString("target_pass", null));
		SMBTransfer.setTargetBase(prefs.getString("target_base", null));
		
		// WakeOnLan 
		String cast = prefs.getString("target_cast", null);
		String mac = prefs.getString("target_mac", null);
		if (cast != null && mac != null) {
			WakeOnLan.sendMagic(cast, mac);
		}
		
		boolean success = true;
		
		Log.i(TAG, "Exporting and transferring contacts...");
		SyncContacts sc = new SyncContacts(context);
		try {
			sc.sync();
		} catch (SyncException se) {
			success = false;
			showNotification(SYNC_CONTACTS, se.title, se.header, se.content);
		}
		Log.i(TAG, "Exporting and transferring contacts...done");

		Log.i(TAG, "Exporting and transferring calendar");
		SyncCalendar scal = new SyncCalendar(context);
		try {
			scal.sync();
		} catch (SyncException se) {
			success = false;
			showNotification(SYNC_CALENDAR, se.title, se.header, se.content);
		}
		Log.i(TAG, "Exporting and transferring calendar...done");

		Log.i(TAG, "Exporting and transferring pictures");
		SyncDirectory spic = new SyncDirectory(prefs.getString("source_path", null));
		try {
			spic.sync();
			
			SharedPreferences.Editor editor = prefs.edit();
		    editor.putLong("last_success", System.currentTimeMillis());
		    editor.commit();
		} catch (SyncException se) {
			success = false;
			showNotification(SYNC_FOLDER, se.title, se.header, se.content);
		}
		Log.i(TAG, "Exporting and transferring pictures...done");
		
		
		if (success) {
			String time = DateFormat.getDateInstance(DateFormat.FULL).format(new Date());
			showNotification(SYNC_DONE, "Sync finished", "Syncing completed", "last successful @ "+time);
		}
		
	}
}
